using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DataGridInsert
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class DataGridInsert : System.Web.UI.Page
	{
        protected System.Web.UI.WebControls.DataGrid grdView;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.Button btnInsert;
	
		ICollection CreateDataSource() 
		{
			DataTable dt = new DataTable();
			DataRow dr;
 
			dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
			dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
			dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
 
			for (int i = 0; i < 9; i++) 
			{
				dr = dt.NewRow();

				if (i == 0)
				{
					// first row will be hidden insert row
					dr[0] = -1;
					dr[1] = "";
					dr[2] = -1;
				}
				else
				{
					dr[0] = i;
					dr[1] = "Item " + i.ToString();
					dr[2] = 1.23 * (i + 1);
				}
 
				dt.Rows.Add(dr);
			}
 
			DataView dv = new DataView(dt);
			return dv;
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack) 
			{
				// Load this data only once.
                ICollection data = CreateDataSource();
                //grdView.DataSource = data;
				//grdView.DataBind();

			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
			this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.OnGridItemCreated);
			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnGridItemCancel);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnGridItemEdit);
			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnGridItemUpdate);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnInsert_Click(object sender, System.EventArgs e)
		{
			// The hidden row at position 0 will be used to enter data
			DataGrid1.EditItemIndex = 0;
			DataGrid1.DataSource= CreateDataSource();
			DataGrid1.DataBind();

			btnInsert.Enabled = false;
		}

		private void OnGridItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if ( e.Item.ItemIndex == 0 ) 
			{
				System.Web.UI.WebControls.LinkButton lb = ( System.Web.UI.WebControls.LinkButton )e.Item.Cells[0].Controls[0];

				if ( lb.Text == "Edit" ) 
				{
					// If this is row 0 and not inserting, hide the row
					lb.Text = "";
					e.Item.Cells[0].Visible = false;
					e.Item.Cells[1].Visible = false;
					e.Item.Cells[2].Visible = false;
					e.Item.Cells[3].Visible = false;
				} 
				else if ( lb.Text == "Update" ) 
				{
					// If inserting, make row visible
					lb.Text = "Insert";
					e.Item.Cells[0].Visible = true;
					e.Item.Cells[1].Visible = true;
					e.Item.Cells[2].Visible = true;
					e.Item.Cells[3].Visible = true;
				}		
			}
			else if (e.Item.ItemIndex > 0)
			{
				// Make first data column read-only for existing items
				System.Web.UI.WebControls.LinkButton lb = ( System.Web.UI.WebControls.LinkButton )e.Item.Cells[0].Controls[0];
				if (lb.Text == "Update")
				{
					if (e.Item.Cells[1].Controls.Count > 0)
						((TextBox)e.Item.Cells[1].Controls[0]).Enabled = false;
				}
			}
		
		}

		private void OnGridItemCancel(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = -1;
			DataGrid1.DataSource= CreateDataSource();
			DataGrid1.DataBind();

			btnInsert.Enabled = true;
		}

		private void OnGridItemEdit(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = e.Item.ItemIndex;
			DataGrid1.DataSource= CreateDataSource();
			DataGrid1.DataBind();

			btnInsert.Enabled = false;
		}

		private void OnGridItemUpdate(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			String scriptString = "";

			if (e.Item.ItemIndex == 0)
			{
				// code here to add new item
				scriptString = "<script language=JavaScript> alert('New Item Added!')";
			}
			else
			{
				// code here to update existing item
				scriptString = "<script language=JavaScript> alert('Item Updated!')";
			}

			scriptString += "<";
			scriptString += "/";
			scriptString += "script>";

			if(!this.IsClientScriptBlockRegistered("clientScript"))
				this.RegisterClientScriptBlock("clientScript", scriptString);

			DataGrid1.EditItemIndex = -1;
			DataGrid1.DataSource= CreateDataSource();
			DataGrid1.DataBind();

			btnInsert.Enabled = true;
		}
	}
}
